import router from "./router";

const web_title = "Xiaomi SU7，Xiaomi 14 Ultra，小米澎湃OS，小米徕卡影像大赛";

if (localStorage.getItem("userInfo")) {
  localStorage.removeItem("userInfo");
}

if (localStorage.getItem("userName")) {
  localStorage.removeItem("userName");
}

const to_login = ["shop/buy/cart", "shop/buy/detail"];

router.beforeEach((to, _from, next) => {
  if (to_login.includes(to.name as string)) {
    if (localStorage.getItem("userInfo")) {
      next();
    } else {
      next("/auth?type=login");
    }
  }

  document.title = web_title;

  if (to.meta?.title) {
    document.title = to.meta.title as string;
  }
  next();
});

router.afterEach((to) => {
  document.title = web_title;

  if (to.meta?.title) {
    document.title = to.meta.title as string;
  }
});

export default router;
